﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using InfoJetSoft.Service;

namespace InfoJetServiceDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        // *** Please reference http://www.infojetsoft.com/service_faq.htm for more information.

        protected void Page_Load(object sender, EventArgs e)
        {
            this.BuildVersionLabel.Text = InfoJetService.BuildVersion;
            this.RegisterTable.Visible = true;
            this.EditTable.Visible = false;
            this.FormTable.Visible = false;
        }

        protected void RegisterTemplateButton_ServerClick(object sender, EventArgs e)
        {
            //Remove the old registered form templates from InfoJet Service, for the trial version of InfoJet Service only could service for one form template.
            //No need to remove the registered form templates while you have one InfoJet Service Enterprise license key.
            if (this.PublishUrlTextBox.Text != null)
            {
                string[] registeredPublishUrls = InfoJetService.List();
                foreach (string registeredPublishUrl in registeredPublishUrls)
                {
                    InfoJetService.Remove(registeredPublishUrl);
                }
            }

            //The method InfoJetService.Register() requires the binary content of the form template(.xsn file) as a parameter.
            byte[] xsnContent = new byte[XSNFile.PostedFile.ContentLength];
            XSNFile.PostedFile.InputStream.Read(xsnContent, 0, xsnContent.Length);

            //Note: Only need to register the form template ONCE.
            //The method InfoJetService.Register() will return the PublishUrl of the form template.
            //In a real application, the returned PublishUrl should be saved in database or other storages.
            //The PublishUrl should be unique in one application.
            //The trial version of InfoJet Service only could service for one form template.
            //If try to register more form templates in the trial version, the method InfoJetService.Register() will return null.
            string PublishUrl = InfoJetService.Register(Context, xsnContent, "~");
            this.PublishUrlTextBox.Text = PublishUrl;

            this.RegisterTable.Visible = false;
            this.EditTable.Visible = true;
            this.FormTable.Visible = false;
        }

        protected void NewFormButton_ServerClick(object sender, EventArgs e)
        {
            string PublishUrl = this.PublishUrlTextBox.Text;
            if (InfoJetService.ContainsXSN(PublishUrl))
            {
                //Create the web form of the template xml document with the publish url.
                InfoJetForm newForm = InfoJetService.BuildFormByTemplate(Context, PublishUrl);
                this.xDocView.Text = newForm.Xhtml;

                this.RegisterTable.Visible = false;
                this.EditTable.Visible = true;
                this.FormTable.Visible = true;
            }
            else
            {
                //If the XSN doesn't exist in InfoJet Service, register it again.
                this.GoToRegisterFormTemplate();
            }
        }

        protected void EditFormButton_ServerClick(object sender, EventArgs e)
        {
            string PublishUrl = this.PublishUrlTextBox.Text;
            if (InfoJetService.ContainsXSN(PublishUrl))
            {
                //The method InfoJetService.BuildFormByXML() requires the string content of an existing xml document as a parameter.
                byte[] xmlContent = new byte[XMLFile.PostedFile.ContentLength];
                XMLFile.PostedFile.InputStream.Read(xmlContent, 0, xmlContent.Length);
                string xml = System.Text.UTF8Encoding.UTF8.GetString(xmlContent);

                InfoJetForm editForm = InfoJetService.BuildFormByXML(Context, PublishUrl, xml);
                xDocView.Text = editForm.Xhtml;

                this.RegisterTable.Visible = false;
                this.EditTable.Visible = true;
                this.FormTable.Visible = true;
            }
            else
            {
                //If the XSN doesn't exist in InfoJet Service, register it again.
                this.GoToRegisterFormTemplate();
            }
        }

        protected void SaveFormButton_ServerClick(object sender, EventArgs e)
        {
            string PublishUrl = this.PublishUrlTextBox.Text;
            if (InfoJetService.ContainsXSN(PublishUrl))
            {
                //The method InfoJetService.ReloadForm is used to rebuild the web form of the xml document submiited by users. 
                InfoJetForm savedForm = InfoJetService.ReloadForm(Context);

                //Write your codes here to save saveForm.Xml to disk or database.

                //To render the saved form view again, please use the following code:
                xDocView.Text = savedForm.Xhtml;

                this.RegisterTable.Visible = false;
                this.EditTable.Visible = true;
                this.FormTable.Visible = true;
            }
            else
            {
                //If the XSN doesn't exist in InfoJet Service, register it again.
                this.GoToRegisterFormTemplate();
            }
        }

        protected void DownloadXMLButton_ServerClick(object sender, EventArgs e)
        {
            string PublishUrl = this.PublishUrlTextBox.Text;
            if (InfoJetService.ContainsXSN(PublishUrl))
            {
                //The method InfoJetService.ReloadForm is used to rebuild the web form of the xml document submiited by users. 
                InfoJetForm savedForm = InfoJetService.ReloadForm(Context);
                //Clear the data in HTTP session cached by the current form while you won't need to render the form again, such as redirecting user to another page.
                //InfoJetService.ClearSession(Context);

                //Write your codes here to save saveForm.Xml to disk or database here.

                //Write the saved xml into the response.
                byte[] xmlContent = System.Text.UTF8Encoding.UTF8.GetBytes(savedForm.Xml);
                this.Response.Clear();
                this.Response.ClearHeaders();
                this.Response.Charset = "UTF-8";
                this.Response.ContentEncoding = System.Text.Encoding.UTF8;
                this.Response.ContentType = "application/octet-stream";
                this.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("InfoJetForm.xml"));
                this.Response.AddHeader("Content-Length", "" + xmlContent.Length);
                this.Response.BinaryWrite(xmlContent);
                this.Response.End();
            }
            else
            {
                //If the XSN doesn't exist in InfoJet Service, register it again.
                this.GoToRegisterFormTemplate();
            }
        }

        protected void RegisterAnotherButton_ServerClick(object sender, EventArgs e)
        {
            this.GoToRegisterFormTemplate();
        }

        protected void GoToRegisterFormTemplate()
        {
            this.PublishUrlTextBox.Text = null;
            this.RegisterTable.Visible = true;
            this.EditTable.Visible = false;
            this.FormTable.Visible = false;
        }

        protected void CloseFormButton_ServerClick(object sender, EventArgs e)
        {
            //While the form editing page is closed, clear the http session data and the temp files used by the form.
            InfoJetService.ClearFormCache(this.Context);
            this.RegisterTable.Visible = false;
            this.EditTable.Visible = true;
            this.FormTable.Visible = false;
        }

        protected void CloseWindowButton_ServerClick(object sender, EventArgs e)
        {
            //While the form editing page is closed, clear the http session data and the temp files used by the form.
            InfoJetService.ClearFormCache(this.Context);
            //While user logoff, clear the http session data and the temp files used by the current user.
            InfoJetService.ClearUserCache(this.Context);
            //Output javascript code to close the browser.
            this.Context.Response.Write("<script language=javascript>window.opener=null; window.close();</script>");
        }
    }
}
